﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using ZetaHtmlTidy;
using System.Xml;
using System.Windows.Forms;

namespace MovieDB
{
    class MoviePosterDBPlugin : IPlugin
    {
        private MpdbImages imgList = new MpdbImages();

        public ToolStripItem GetMenuItems()
        {
            ToolStripItem item = new ToolStripMenuItem();
            item.Text = "MoviePosterDB";
            item.Click += new EventHandler(item_Click);

            return item;
        }

        void item_Click(object sender, EventArgs e)
        {
            MessageBox.Show("TST");
        }

        public MpdbImages GetImageList(string imdb)
        {
            WebClient wc = new WebClient();

            if (imdb.Substring(0, 2).ToLower() == "tt")
            {
                imdb = imdb.Substring(2);
            }

            string content = wc.DownloadString("http://www.movieposterdb.com/movie/" + imdb).Replace("nbsp", "");
            //// Quelltext in XHTML umwandeln
            //// und in ein XMLDocument Objekt speichern
            HtmlTidy tidy = new HtmlTidy();
            XmlDocument xmldoc = new XmlDocument();
            List<string> picPagesList = new List<string>();
            List<string> picList = new List<string>();
            xmldoc.XmlResolver = null;
            xmldoc.LoadXml(tidy.CleanHtml(content, HtmlTidyOptions.ConvertToXhtml));

            foreach (XmlNode node in xmldoc.GetElementsByTagName("td"))
            {
                if (node.Attributes.Count > 0 && node.Attributes["class"] != null && node.Attributes["class"].InnerText == "poster")
                {

                    picPagesList.Add(node.ChildNodes[0].ChildNodes[0].Attributes[0].InnerText);
                }
            }

            foreach (string picSite in picPagesList)
            {
                content = wc.DownloadString(picSite).Replace("nbsp", "");
                xmldoc.LoadXml(tidy.CleanHtml(content, HtmlTidyOptions.ConvertToXhtml));

                foreach (XmlNode node in xmldoc.GetElementsByTagName("td"))
                {
                    if (node.Attributes.Count > 0 && node.Attributes["class"] != null && node.Attributes["class"].InnerText == "poster")
                    {
                        string url = node.ChildNodes[0].ChildNodes[0].Attributes[0].InnerText;
                        XmlDocument doc = new XmlDocument();
                        doc.XmlResolver = null;
                        content = wc.DownloadString(url).Replace("nbsp", "");
                        doc.LoadXml(tidy.CleanHtml(content, HtmlTidyOptions.ConvertToXhtml));

                        foreach (XmlNode image in doc.GetElementsByTagName("img"))
                        {
                            if (image.Attributes.Count > 0 && image.Attributes["id"] != null && image.Attributes["id"].InnerText == "poster_image")
                            {
                                string imgUrl = image.Attributes["src"].InnerText;
                                WebRequest requestPic = WebRequest.Create(imgUrl);
                                WebResponse responsePic = requestPic.GetResponse();
                                Image imgTemp = Image.FromStream(responsePic.GetResponseStream());

                                this.imgList.ImageList.Add(imgTemp);
                                this.imgList.ImageNames.Add(imgUrl.Substring(imgUrl.LastIndexOf("/") + 1));
                            }
                        }
                        break;
                    }
                }

                foreach (XmlNode image in xmldoc.GetElementsByTagName("img"))
                {
                    if (image.Attributes.Count > 0 && image.Attributes["id"] != null && image.Attributes["id"].InnerText == "poster_image")
                    {
                        string imgUrl = image.Attributes["src"].InnerText;
                        WebRequest requestPic = WebRequest.Create(imgUrl);
                        WebResponse responsePic = requestPic.GetResponse();
                        Image imgTemp = Image.FromStream(responsePic.GetResponseStream());

                        this.imgList.ImageList.Add(imgTemp);
                        this.imgList.ImageNames.Add(imgUrl.Substring(imgUrl.LastIndexOf("/") + 1));
                        break;
                    }
                }

            }

            return this.imgList;
        }

        public string Name
        {
            get
            {
                return "MoviePosterDBPlugin.dll";
            }
        }
    }
}
